scan_fmt
scan_fmt provides a simple scanf()-like input for Rust. The goal is to make it easier to read data from a string or stdin.
Currently the format string supports the following special sequences:
Examples
#[macro_use] extern crate scan_fmt;
use std::error::Error ;
fn main() -> Result<(),Box<dyn Error>> {
let (a,b,c) = scan_fmt!( "hello 0x12 345 bye", "hello {x} {} {}", [hex u8], i32, String) ? ; assert_eq!( a, 0x12 ) ;
assert_eq!( b, 345 ) ;
assert_eq!( c, "bye" ) ;
println!("Enter something like: 123-22");
let (c,d) = scanln_fmt!( "{d}-{d}", u16, u8) ? ; println!("Got {} and {}",c,d) ;
let (a,b) = scan_fmt_some!( "hello 12 345", "hello {} {}", u8, i32) ; assert_eq!( a, Some(12) ) ;
assert_eq!( b, Some(345) ) ;
Ok(())
}
Limitations
There is no compile-time warning if the number of {}'s in the format string doesn't match the number of return values. You'll just get None for extra return values. See src/lib.rs for more details.